/**
 * Copyright &copy; 2013-2016 山东易科德软件有限公司 All rights reserved.
 */
package cn.net.ecode.modules.cms.service;

import java.util.List;

import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;

import cn.net.ecode.common.persistence.Page;
import cn.net.ecode.common.service.TreeService;
import cn.net.ecode.common.utils.StringUtils;
import cn.net.ecode.modules.cms.dao.CategoryDao;
import cn.net.ecode.modules.cms.entity.Category;
import cn.net.ecode.modules.cms.entity.Site;
import cn.net.ecode.modules.cms.utils.CmsUtils;
import cn.net.ecode.modules.sys.entity.Role;

/**
 * 栏目Service
 * @author ThinkGem
 * @version 2013-5-31
 */
@Service
@Transactional(readOnly = true)
public class CategoryService extends TreeService<CategoryDao, Category> {

	@Autowired
	private ArticleService articleService;
	@Autowired
	private LinkService linkService;
	
	/**
	 * 获取栏目
	 */
	public Category get(Category category) {
		return super.get(category);
	}
	
	/**
	 * 查询栏目
	 */
	public List<Category> findList(Category category) {
		return super.findList(category);
	}

	/**
	 * 查询授权的栏目
	 */
	public List<Category> findList(Category category, boolean isDataScopeFilter) {
		if (isDataScopeFilter && !category.getCurrentUser().isAdmin()){
			List<String> roleCodeList = Lists.newArrayList();
			List<Role> roleList = category.getCurrentUser().getRoleList();
			for (Role role : roleList) {
				if(role != null){
					roleCodeList.add(role.getRoleCode());
				}
			}
			category.setRoleCodeList(roleCodeList);
		}
		return super.findList(category);
	}
	
	/**
	 * 查询栏目
	 */
	@Deprecated
	public List<Category> findList(boolean isCurrentSite, String module){
		Category category = new Category();
		if (isCurrentSite){
			category.setSite(new Site(Site.getCurrentSiteCode()));
		}
		category.setModule(module);
		return findList(category, false);
	}
	
	/**
	 * 查询授权的栏目
	 */
	@Deprecated
	public List<Category> findAuthList(boolean isCurrentSite, String module) {
		Category category = new Category();
		if (isCurrentSite){
			category.setSite(new Site(Site.getCurrentSiteCode()));
		}
		category.setModule(module);
		return findList(category, false);
	}
	
	/**
	 * 分页查询栏目
	 */
	public Page<Category> findPage(Page<Category> page, Category category) {
		return super.findPage(page, category);
	}

	/**
	 * 通过编号获取栏目列表(返回列表，保留categoryCodes的顺序)
	 */
	public List<Category> findListByCodes(String categoryCodes) {
		List<Category> list = Lists.newArrayList();
		String[] codes = StringUtils.split(categoryCodes,",");
		if (codes != null && codes.length>0){
			Category category = new Category();
			category.setCategoryCodeList(Lists.newArrayList(codes));
			List<Category> categoryList = findList(category);
			for (String code : codes){
				for (Category e : categoryList){
					if (e.getCategoryCode().equals(code)){
						list.add(e);
						break;
					}
				}
			}
		}
		return list;
	}

	/**
	 * 根据父级编码查询栏目
	 */
	@Deprecated
	public List<Category> findListByParentCode(String parentCode, String siteCode){
		Category entity = new Category(parentCode);
		entity.setParent(new Category(parentCode));
		entity.setSite(new Site(siteCode));
		return dao.findList(entity);
	}

	/**
	 * 保存栏目
	 */
	@Transactional(readOnly = false)
	public void save(Category category) {
		if (category.getIsNewRecord()){
			//genIdAndValid(category, category.getViewCode());
		}
		category.setSiteCode("1");//默认站点
		category.setInMenu("0");
		category.setInList("0");
		category.setModule("article");
		category.setViewCode(category.getCategoryCode());
		if (StringUtils.isNotBlank(category.getViewConfig())){
            category.setViewConfig(StringEscapeUtils.unescapeHtml4(category.getViewConfig()));
        }
		super.save(category);
		CmsUtils.removeCache("mainNavList_"+category.getSite().getId());
	}

	/**
	 * 更新栏目排序
	 */
	@Transactional(readOnly = false)
	public int updateSorts(Category category) {
		int columns = dao.updateSorts(category);
		CmsUtils.removeCache("mainNavList_"+Site.getCurrentSiteCode());
		return columns;
	}

	/**
	 * 删除栏目
	 */
	@Transactional(readOnly = false)
	public void delete(Category category) {
		super.delete(category);
		CmsUtils.removeCache("mainNavList_"+category.getSite().getSiteCode());
	}
	
}
